package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.ShopImgListAdapter;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.City;
import com.hcinfos.gsoo.enity.Image;
import com.hcinfos.gsoo.enity.UrlImage;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.enity.ViewSuppSearchExt;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.json.ImageListJson;
import com.hcinfos.gsoo.util.LogUtil;

/*
 * 店铺图片详细列表
 */
public class ShopImgListActivity extends Activity implements OnClickListener {
	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;
	private ViewPager viewPager;
	private LinearLayout llayout;
	private int currentItem;
	private ImageView[] imgs;

	private ShopImgListAdapter adapter;
	private List<Image> list;
	private Context context;
	private String shopId;

	private final int DATA_GET_ERROR = 0x1120;
	private final int DATA_GET_EMPTY = DATA_GET_ERROR + 1;
	private final int DATA_GET_SUCCESS = DATA_GET_ERROR + 2;

	private ViewSuppSearchExt detail;
	private RelativeLayout request_rl;
	private RelativeLayout img_rl;
	private TextView status_tv;

	private String cityId = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		context = this;
		setContentView(R.layout.activity_shopimg_list);
		City city = GsooApplication.currentCity;
		if (city != null)
			cityId = city.getCityId();
		initRes();

	}

	private void initRes() {
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		left_ib.setOnClickListener(this);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("店铺一角");

		viewPager = (ViewPager) findViewById(R.id.pager);
		llayout = (LinearLayout) findViewById(R.id.llayout);
		list = new ArrayList<Image>();
		adapter = new ShopImgListAdapter(list, this);
		viewPager.setAdapter(adapter);

		img_rl = (RelativeLayout) findViewById(R.id.img_rl);
		request_rl = (RelativeLayout) findViewById(R.id.request_rl);
		request_rl.setOnClickListener(this);
		status_tv = (TextView) findViewById(R.id.status_tv);
	}

	@Override
	protected void onResume() {
		super.onResume();
		list.clear();
		detail = (ViewSuppSearchExt) getIntent().getSerializableExtra(
				"shopDetail");
		if (detail == null) {
			// do something
			return;
		}
		shopId = detail.getId();
		if (detail.getDataType() != null
				&& detail.getDataType().equals(ViewSuppSearchExt.TYPES[1])) {
			List<UrlImage> images = detail.getPhotos();
			list.clear();
			for (UrlImage image : images) {
				Image img = new Image();
				img.setWebUrl(image.getUrl());
				list.add(img);
			}
			adapter.setIsSelefData(false);
			adapter.setDataChange(list);
		} else {
			obtainImgList();
		}

	}

	private int page = 1;
	private int rows = 20;

	private void obtainImgList() {
		new Thread(new Runnable() {

			@Override
			public void run() {

				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							context, Urls.SHOP_IMG_LIST + "suppId=" + shopId
									+ "&page=" + page + "&rows=" + rows);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = DATA_GET_ERROR;
						handler.sendMessage(msg);
					} else {
						list = ImageListJson.getShopImgListByJson(result);
						if (list.size() > 0) {
							Message msg = handler.obtainMessage();
							msg.what = DATA_GET_SUCCESS;
							handler.sendMessage(msg);
						} else {
							Message msg = handler.obtainMessage();
							msg.what = DATA_GET_EMPTY;
							handler.sendMessage(msg);
						}
					}
				} catch (Exception e) {
					LogUtil.d("wxf", "e:" + e.toString());
				}
			}
		}).start();
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case DATA_GET_ERROR:
				LogUtil.d("wxf", "DATA_GET_ERROR");
				break;
			case DATA_GET_EMPTY:
				img_rl.setVisibility(View.GONE);
				request_rl.setVisibility(View.VISIBLE);
				status_tv.setText("该商铺还没有图片，点击我上传吧");
				LogUtil.d("wxf", "DATA_GET_EMPTY");
				break;
			case DATA_GET_SUCCESS:
				adapter.setDataChange(list);
				viewPager.setCurrentItem(0);
				imgs = new ImageView[list.size()];
				for (int i = 0; i < list.size(); i++) {
					ImageView view = new ImageView(context);
					view.setBackgroundResource(R.drawable.page_indicator_bg);

					LinearLayout.LayoutParams parmas = new LinearLayout.LayoutParams(
							12, 12);
					parmas.leftMargin = 5;
					parmas.rightMargin = 5;
					view.setLayoutParams(parmas);
					llayout.addView(view);
					imgs[i] = (ImageView) llayout.getChildAt(i);
					imgs[i].setEnabled(true);
					imgs[i].setTag(i);
				}
				currentItem = 0;
				imgs[currentItem].setEnabled(false);
				viewPager.setOnPageChangeListener(new MyPageChangeListener());
				break;
			}
		};
	};

	/**
	 * viewpage listener
	 */
	private class MyPageChangeListener implements OnPageChangeListener {
		private int oldPosition = 0;

		public void onPageSelected(int position) {
			imgs[currentItem].setEnabled(true);
			imgs[position].setEnabled(false);
			currentItem = position;
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.request_rl:
			Intent intent = new Intent(ShopImgListActivity.this,
					UploadImgActivity.class);
			intent.putExtra("shopDetail", detail);
			startActivity(intent);
			finish();
			break;
		}
	}

}
